﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormPowerModify : Form
    {
        private static FormPowerModify formPowerModify = null;
        private static int id = 0;
        public FormPowerModify()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            string remark = FormRolePower.powersModel.Remark;
            int groupId = FormRolePower.powersModel.PowerGroupId;
            groupBox_PowerUpdate.Text = "权限【" + remark + "】修改";
            textBox_Remark.Text = remark;
            id = FormRolePower.powersModel.Id;
            PowersBLL powersBLL = new PowersBLL();
            List<PowerGroupModel> powerGroup = powersBLL.GetPowerGroup();
            List<TextAndValue> items = new List<TextAndValue>();
            int selectIndex = 0;
            for (int i = 0; i < powerGroup.Count; i++)
            {
                if (powerGroup[i].Id == groupId)
                {
                    selectIndex = i;
                }
                items.Add(new TextAndValue(powerGroup[i].GroupName, powerGroup[i].Id.ToString()));
            }

            comboBox_GroupName.DataSource = items;
            comboBox_GroupName.DisplayMember = "DisplayText";
            comboBox_GroupName.ValueMember = "RealValue";
            comboBox_GroupName.SelectedIndex = selectIndex;
        }


        public static FormPowerModify CreateInstance()
        {
            if (formPowerModify == null || formPowerModify.IsDisposed)
            {
                formPowerModify = new FormPowerModify();
            }
            return formPowerModify;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button_Sure_Click(object sender, EventArgs e)
        {
            int groupId = Convert.ToInt32(comboBox_GroupName.SelectedValue);
            string remark = textBox_Remark.Text.Trim();
            if (string.IsNullOrEmpty(remark))
            {
                MessageBox.Show("权限名称不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            
            PowersBLL powers = new PowersBLL();
            int num = Convert.ToInt32(powers.GetPowerForName(remark, id));
            if(num > 0)
            {
                MessageBox.Show("该名称已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int result = powers.UpdatePower(id, remark, groupId);
            if(result > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("修改失败，请稍后再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
    }
}
